package com.topisv.tms.service.stock.instorage;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.dto.OutstorageInfoDto;
import com.topisv.tms.entity.stock.instorage.OutstorageInfo;
import com.topisv.tms.repository.stock.instorage.OutstorageInfoDao;

/**
 * 出库信息_service
 * @author ch
 *
 */
@Component
@Transactional(readOnly = true)
public class OutstorageInfoService {
	
	@Autowired
	private OutstorageInfoDao outstorageInfoDao;

	/**
	 * 出库信息dto查询
	 * @param param
	 * @return
	 */
	public List<OutstorageInfoDto> loadDtoByParam(Map<String, Object> param) {
		
		 String customerCode=(String) param.get("customerCode");
		 String warehouseCode=(String) param.get("warehouseCode");
		
		Map<String, Integer> map=new HashMap<String, Integer>();
		List<OutstorageInfoDto> dtoList=new ArrayList<OutstorageInfoDto>();
		List<OutstorageInfo> infoList=this.loadByParam(param);
		
		for (OutstorageInfo info : infoList) {
			String sku=info.getSku();
			int num=info.getNum();
			if(map.keySet().contains(sku)){
				map.put(sku,map.get(sku)+num);
			}else{
				map.put(sku, num);
			}
		}
		
	    for(String sku:map.keySet()){
	    	OutstorageInfoDto dto=new OutstorageInfoDto();
	    	dto.setCustomerCode(customerCode);
	    	dto.setWarehouseCode(warehouseCode);
	    	dto.setSku(sku);
	    	dto.setNum(map.get(sku));
	    	dtoList.add(dto);
	    }
		return dtoList;
	}
	
	
	/**
	 * 查找出库信息
	 * @param param 查询参数
	 * @return
	 */
	public List<OutstorageInfo> loadByParam(Map<String, Object> param) {
		Specification<OutstorageInfo> spec = getWhereClause(param);
		List<OutstorageInfo> infoList = this.outstorageInfoDao.findAll(spec);
		return infoList;
	}
	
	/**
	 * 查询条件封装
	 * @param param
	 * @return
	 */
	private Specification<OutstorageInfo> getWhereClause(final Map<String, Object> param) {
		
		return new Specification<OutstorageInfo>(){
			
		    @Override
		    public Predicate toPredicate(Root<OutstorageInfo> r, CriteriaQuery<?> q, CriteriaBuilder cb) {
		      Predicate pre = cb.conjunction();
		      
		      String customerCode=(String) param.get("customerCode");
		      if(StringUtils.isNotBlank(customerCode)){
					pre.getExpressions().add(cb.equal(r.get("customerCode"),customerCode.trim()));
			  }
		      
		      String warehouseCode=(String) param.get("warehouseCode");
		      if(StringUtils.isNotBlank(warehouseCode)){
					pre.getExpressions().add(cb.equal(r.get("warehouseCode"),warehouseCode.trim()));
			  }

		      String sku=(String) param.get("sku");
		      if(StringUtils.isNotBlank(sku)){
					pre.getExpressions().add(cb.equal(r.get("sku"),sku));
			  }	
		      
		      String batchCode=(String) param.get("batchCode");
		      if(StringUtils.isNotBlank(batchCode)){
					pre.getExpressions().add(cb.equal(r.get("batchCode"),batchCode));
			  }	
		      
		      String taskCode=(String) param.get("taskCode");
		      if(StringUtils.isNotBlank(taskCode)){
					pre.getExpressions().add(cb.equal(r.get("taskCode"),taskCode));
			  }	
		      
		      Date fromDate= (Date) param.get("fromDate");
		      if (fromDate != null) {
		        pre.getExpressions().add(cb.greaterThanOrEqualTo(r.<Date>get("opeTime"), fromDate));
		      }
		      
		      Date toDate= (Date) param.get("toDate");
		      if (toDate != null) {
		        pre.getExpressions().add(cb.lessThanOrEqualTo(r.<Date>get("opeTime"), toDate));
		      }
		      
		      return pre;
		    }
		  };
       }


	/**
	 * 查询出库信息
	 * @param taskCode 拣货任务编号
	 * @return
	 */
	public OutstorageInfo loadByTaskCode(String taskCode,String sku) {
		return this.outstorageInfoDao.loadByTaskCode(taskCode,sku);
	}


	/**
	 * 保存
	 * @param info
	 */
	@Transactional(readOnly = true)
	public void save(OutstorageInfo info) {
		this.outstorageInfoDao.save(info);
	}
	
	
	
}
